package cl.clientesws;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;

public class PublicacionesSOAWS {
	
	private static String METHOD_NAME ;
	private static String NAMESPACE ;
	private static String URL;
	/*
	 *
	 */
	public void coneccionwsPropio(){
		NAMESPACE = "http://ws";
		METHOD_NAME = "add";
		URL = "http://192.168.1.2:8080/TallerSOA/services/PublicacionSOA";

	}

	/**
	 *
	 * @param rut
	 * @param verificador
	 * @param nombre
	 * @param sexo
	 * @return
	 */
	public String addPublicacion(String pub_mon, String pub_des, String pers_rut){
		coneccionwsPropio();
		METHOD_NAME = "add";
		String SOAP_ACTION = NAMESPACE + METHOD_NAME;
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		PropertyInfo addMonto = new PropertyInfo();
		PropertyInfo addDescripcion = new PropertyInfo();
		PropertyInfo addRut = new PropertyInfo();
	
		addMonto.setName("pub_mon");
		addMonto.setValue(pub_mon);
		addMonto.setType(String.class);
		addDescripcion.setName("pub_des");
		addDescripcion.setValue(pub_des);
		addDescripcion.setType(String.class);
		addRut.setName("pers_rut");
		addRut.setValue(pers_rut);
		addRut.setType(String.class);
		
	
		request.addProperty(addMonto);
		request.addProperty(addDescripcion);
		request.addProperty(addRut);
		
		SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		soapEnvelope.dotNet = true;
		soapEnvelope.implicitTypes =true;
		soapEnvelope.setOutputSoapObject(request);
		AndroidHttpTransport aht = new AndroidHttpTransport(URL);
		String datosJsonString;
		try
		{
			aht.call(SOAP_ACTION, soapEnvelope);
			SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();
			datosJsonString =resultString.toString();
			return datosJsonString;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			// en caso de no tener acceso al web service
			datosJsonString = "Error Conexion";}
		return datosJsonString;
	}


	public String updatePublicacion(String id, int campo, String nuevoValor
			){
		coneccionwsPropio();
		METHOD_NAME = "update";
		String SOAP_ACTION = NAMESPACE + METHOD_NAME;
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		PropertyInfo updateId = new PropertyInfo();
		PropertyInfo updatecampo= new PropertyInfo();
		PropertyInfo updatenuevoValor = new PropertyInfo();

		updateId.setName("id");
		updateId.setValue(id);
		updateId.setType(String.class);
		updatecampo.setName("campo");
		updatecampo.setValue(campo);
		updatecampo.setType(int.class);
		updatenuevoValor.setName("nuevoValor");
		updatenuevoValor.setValue(nuevoValor);
		updatenuevoValor.setType(String.class);

		request.addProperty(updateId);
		request.addProperty(updatecampo);
		request.addProperty(updatenuevoValor);

		SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		soapEnvelope.dotNet = true;
		soapEnvelope.implicitTypes =true;
		soapEnvelope.setOutputSoapObject(request);
		AndroidHttpTransport aht = new AndroidHttpTransport(URL);
		String datosJsonString;
		try
		{
			aht.call(SOAP_ACTION, soapEnvelope);
			SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();
			datosJsonString =resultString.toString();
			return datosJsonString;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			// en caso de no tener acceso al web service
			datosJsonString = "Error Conexion";}
		return datosJsonString;
	}
	public String borrarPublicacion(String id){
		coneccionwsPropio();
		METHOD_NAME = "borrar";
		String SOAP_ACTION = NAMESPACE + METHOD_NAME;
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		PropertyInfo borrarId = new PropertyInfo();


		borrarId.setName("id");
		borrarId.setValue(id);
		borrarId.setType(String.class);


		request.addProperty(borrarId);


		SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		soapEnvelope.dotNet = true;
		soapEnvelope.implicitTypes =true;
		soapEnvelope.setOutputSoapObject(request);
		AndroidHttpTransport aht = new AndroidHttpTransport(URL);
		String datosJsonString;
		try
		{
			aht.call(SOAP_ACTION, soapEnvelope);
			SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();
			datosJsonString =resultString.toString();
			return datosJsonString;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			// en caso de no tener acceso al web service
			datosJsonString = "Error Conexion";}
		return datosJsonString;
	}      

	public String findPublicacion(String buscar){
		coneccionwsPropio();
		METHOD_NAME = "find";
		String SOAP_ACTION = NAMESPACE + METHOD_NAME;
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		PropertyInfo findBuscar = new PropertyInfo();


		findBuscar.setName("buscar");
		findBuscar.setValue(buscar);
		findBuscar.setType(String.class);


		request.addProperty(findBuscar);


		SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		soapEnvelope.dotNet = true;
		soapEnvelope.implicitTypes =true;
		soapEnvelope.setOutputSoapObject(request);
		AndroidHttpTransport aht = new AndroidHttpTransport(URL);
		String datosJsonString;
		try
		{
			aht.call(SOAP_ACTION, soapEnvelope);
			SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();
			datosJsonString =resultString.toString();
			return datosJsonString;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			// en caso de no tener acceso al web service
			datosJsonString = "Error Conexion";}
		return datosJsonString;
	}      
	public String finAllPublicacion(String buscar){
		coneccionwsPropio();
		METHOD_NAME = "borrar";
		String SOAP_ACTION = NAMESPACE + METHOD_NAME;
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		PropertyInfo findBuscar = new PropertyInfo();


		findBuscar.setName("buscar");
		findBuscar.setValue(buscar);
		findBuscar.setType(String.class);


		request.addProperty(findBuscar);


		SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		soapEnvelope.dotNet = true;
		soapEnvelope.implicitTypes =true;
		soapEnvelope.setOutputSoapObject(request);
		AndroidHttpTransport aht = new AndroidHttpTransport(URL);
		String datosJsonString;
		try
		{
			aht.call(SOAP_ACTION, soapEnvelope);
			SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();
			datosJsonString =resultString.toString();
			return datosJsonString;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			// en caso de no tener acceso al web service
			datosJsonString = "Error Conexion";}
		return datosJsonString;
	} 
}
